package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.WorkflowBusinessNodeRpcService;
import com.hunttown.mes.rpc.domain.WorkflowBusinessNodeDTO;
import com.hunttown.mes.rpc.domain.query.WorkflowBusinessNodeDTOQuery;
import com.hunttown.mes.service.WorkflowBusinessNodeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.WorkflowBusinessNodeConverts.convert;

/**
 * created by wangjunfu on 2019-08-01 16:55:25
 */
@Service
public class WorkflowBusinessNodeRpcServiceImpl implements WorkflowBusinessNodeRpcService {

    private final Logger logger = LoggerFactory.getLogger(WorkflowBusinessNodeRpcServiceImpl.class);

    private final WorkflowBusinessNodeService workflowBusinessNodeService;

    @Autowired
    public WorkflowBusinessNodeRpcServiceImpl(WorkflowBusinessNodeService workflowBusinessNodeService) {
        this.workflowBusinessNodeService = workflowBusinessNodeService;
    }

    @Override
    public WorkflowBusinessNodeDTO insert(WorkflowBusinessNodeDTO workflowBusinessNodeDTO) {
        try {
            return convert(workflowBusinessNodeService.insert(convert(workflowBusinessNodeDTO)));
        } catch (Exception e) {
            logger.error("WorkflowBusinessNode create failed.", e);
            throw new RuntimeException("WorkflowBusinessNode create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(WorkflowBusinessNodeDTO workflowBusinessNodeDTO) {
        try {
            return workflowBusinessNodeService.updateInfoById(convert(workflowBusinessNodeDTO));
        } catch (Exception e) {
            logger.error("WorkflowBusinessNode update by id failed.", e);
            throw new RuntimeException("WorkflowBusinessNode update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(WorkflowBusinessNodeDTOQuery workflowBusinessNodeDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (workflowBusinessNodeDTOQuery.getW_id() != null && workflowBusinessNodeDTOQuery.getW_id() > 0) {
                query.put("w_id", workflowBusinessNodeDTOQuery.getW_id());
            }

            if (workflowBusinessNodeDTOQuery.getW_deleteFlag() != null) {
                query.put("w_deleteFlag", workflowBusinessNodeDTOQuery.getW_deleteFlag());
            }

            if (workflowBusinessNodeDTOQuery.getW_businessId() != null) {
                query.put("w_businessId", workflowBusinessNodeDTOQuery.getW_businessId());
            }

            if (workflowBusinessNodeDTOQuery.getIds() != null && !workflowBusinessNodeDTOQuery.getIds().equals("")) {
                query.put("ids", workflowBusinessNodeDTOQuery.getIds());
            }

            if (workflowBusinessNodeDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", workflowBusinessNodeDTOQuery.getStopFlag());
            }

            if (workflowBusinessNodeDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", workflowBusinessNodeDTOQuery.getDeleteFlag());
            }

            return workflowBusinessNodeService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("WorkflowBusinessNode update by query failed.", e);
            throw new RuntimeException("WorkflowBusinessNode update by query failed, Please order your code.", e);
        }
    }

    @Override
    public WorkflowBusinessNodeDTO selectById(Integer id) {
        try {
            return convert(workflowBusinessNodeService.selectById(id));
        } catch (Exception e) {
            logger.error("WorkflowBusinessNode select by id failed.", e);
            throw new RuntimeException("WorkflowBusinessNode select by id failed, Please order your code.", e);
        }
    }

    @Override
    public WorkflowBusinessNodeDTO selectObjByQuery(WorkflowBusinessNodeDTOQuery workflowBusinessNodeDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (workflowBusinessNodeDTOQuery.getBusinessId() != null && workflowBusinessNodeDTOQuery.getBusinessId() > 0) {
                query.put("businessId", workflowBusinessNodeDTOQuery.getBusinessId());
            }

            if (StringUtils.isNotBlank(workflowBusinessNodeDTOQuery.getOrderBy())) {
                query.put("orderBy", workflowBusinessNodeDTOQuery.getOrderBy());
            }

            if (workflowBusinessNodeDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", workflowBusinessNodeDTOQuery.getStopFlag());
            }

            if (workflowBusinessNodeDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", workflowBusinessNodeDTOQuery.getDeleteFlag());
            }

            return convert(workflowBusinessNodeService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("WorkflowBusinessNode select obj by query failed.", e);
            throw new RuntimeException("WorkflowBusinessNode select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<WorkflowBusinessNodeDTO> getListForPage(WorkflowBusinessNodeDTOQuery workflowBusinessNodeDTOQuery) {
        Query query = Query.page(workflowBusinessNodeDTOQuery.getPn(), workflowBusinessNodeDTOQuery.getPageSize());

        if (workflowBusinessNodeDTOQuery.getId() != null && workflowBusinessNodeDTOQuery.getId() > 0) {
            query.put("id", workflowBusinessNodeDTOQuery.getId());
        }

        if (workflowBusinessNodeDTOQuery.getBusinessId() != null && workflowBusinessNodeDTOQuery.getBusinessId() > 0) {
            query.put("businessId", workflowBusinessNodeDTOQuery.getBusinessId());
        }

        if (workflowBusinessNodeDTOQuery.getIds() != null && !workflowBusinessNodeDTOQuery.getIds().equals("")) {
            query.put("ids", workflowBusinessNodeDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(workflowBusinessNodeDTOQuery.getLikeName())) {
            query.put("likeName", workflowBusinessNodeDTOQuery.getLikeName());
        }

        if (workflowBusinessNodeDTOQuery.getIsJudge() != null) {
            query.put("isJudge", workflowBusinessNodeDTOQuery.getIsJudge());
        }

        if (StringUtils.isNotBlank(workflowBusinessNodeDTOQuery.getOrderBy())) {
            query.put("orderBy", workflowBusinessNodeDTOQuery.getOrderBy());
        }

        if (workflowBusinessNodeDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", workflowBusinessNodeDTOQuery.getStopFlag());
        }

        if (workflowBusinessNodeDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", workflowBusinessNodeDTOQuery.getDeleteFlag());
        }

        List<WorkflowBusinessNodeDTO> list = convert(workflowBusinessNodeService.selectByQuery(query));
        Page<WorkflowBusinessNodeDTO> page = Page.from(workflowBusinessNodeDTOQuery.getPn(), workflowBusinessNodeDTOQuery.getPageSize(), list);
        page.setTotalItems(workflowBusinessNodeService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return workflowBusinessNodeService.deleteById(id);
        } catch (Exception e) {
            logger.error("WorkflowBusinessNode delete failed.", e);
            throw new RuntimeException("WorkflowBusinessNode delete failed, Please order your code.", e);
        }
    }
}